import { getBilibiliList } from './bilibili.js'
const getTiebaListApi = async (id) => {
  try {
    let res = await fetch("https://tieba.baidu.com/hottopic/browse/topicList");
    let data = await res.json();
    let arr = data.data.bang_topic.topic_list.map((item, index) => {
      return {
        url: item.topic_url,
        index: item.idx_num,
        detail: item.topic_desc,
        hot: item.discuss_num,
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    return {
      id,
      list: []
    };
  }
};

const getShaoshuListApi = async (id) => {
  try {
    let res = await fetch(
      "https://sspai.com/api/v1/article/matrix/hot/page/get?limit=50&offset=0"
    );
    let data = await res.json();
    let arr = data.data.map((item, index) => {
      return {
        url: `https://sspai.com/post/${item.id}`,
        index: index + 1,
        detail: item.title,
        summary: item.summary,
        hot: "",
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    return {id, list: []};
  }
};

const getJuejinListApi = async (id) => {
  try {
    let res = await fetch(
      "https://api.juejin.cn/content_api/v1/content/article_rank?category_id=1&type=hot&spider=0"
    );
    let data = await res.json();
    let arr = data.data.map((item, index) => {
      return {
        url: `https://juejin.cn/post/${item.content.content_id}`,
        index: index + 1,
        detail: item.content.title,
        hot: item.content_counter.hot_rank,
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    return {id, list: []};
  }
};

const getZhihuListApi = async (id) => {
  try {
    let res = await fetch(
      "https://www.zhihu.com/api/v4/creators/rank/hot?domain=0&period=day"
    );
    let data = await res.json();
    let arr = data.data.map((item, index) => {
      return {
        url: `https://www.zhihu.com/question/${item.question.token}`,
        index: index + 1,
        detail: item.question.title,
        hot: item.reaction.new_pv,
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    return {id, list: []};
  }
};

const getJinritoutiaoListApi = async (id) => {
  try {
    let res = await fetch(
      "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc"
    );
    let data = await res.json();
    let arr = data.data.map((item, index) => {
      return {
        url: item.Url,
        index: index + 1,
        detail: item.Title,
        hot: item.HotValue,
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    return {id, list: []};
  }
};

const getBilibiliListApi = async (id) => {
  try {
    let res = await fetch(
      "https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all"
    );
    let data = await res.json();
    let arr = data.data.list.map((item, index) => {
      return {
        url: `https://www.bilibili.com/video/${item.bvid}`,
        index: index + 1,
        detail: item.title,
        hot: item.stat.vv,
      };
    });
    return {
      id,
      list: arr
    }
  } catch (error) {
    let res = await getBilibiliList(id);
    return res;
  }
};

export {
  getTiebaListApi,
  getShaoshuListApi,
  getJuejinListApi,
  getZhihuListApi,
  getJinritoutiaoListApi,
  getBilibiliListApi,
};
